import { Injectable } from '@angular/core';
import { Action, NgxsOnInit, State, StateContext } from '@ngxs/store';
import { MessageAction } from '@shared/lib/store/message/message.action';

export interface MessageStateModel {
  msg: any;
}

@State<MessageStateModel>({
  name: 'message',
  defaults: {
    msg: {},
  },
})
@Injectable({
  providedIn: 'root',
})
export class MessageState implements NgxsOnInit {
  constructor() {
  }

  @Action(MessageAction.Send)
  Send(ctx: StateContext<MessageStateModel>, {data}: MessageAction.Send) {
    ctx.patchState({msg: data});
    ctx.patchState({msg: undefined});
  }

  ngxsOnInit(ctx: StateContext<any>): void {
    let state = ctx.getState();
    if (state.msg) ctx.patchState({msg: undefined});
  }
}
